package cn.liziguo.tank.collision;

import cn.liziguo.tank.config.GameConfig;
import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.constants.ItemType;
import cn.liziguo.tank.constants.PropertyConst;
import cn.liziguo.tank.constants.SpawnsConst;
import cn.liziguo.tank.factory.MapFactory;
import cn.liziguo.tank.script.player.PlayerComponent;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import com.almasb.fxgl.time.TimerAction;

import java.util.List;

/**
 * @author Liziguo
 * @date 2022-07-09 21:26:11
 */
public class ItemPlayerCollisionHandler extends CollisionHandler {

    private TimerAction freezeEnemyTimerAction;

    private List<Entity> spadeList;

    public ItemPlayerCollisionHandler() {
        super(GameEnum.ITEM, GameEnum.PLAYER);
    }

    @Override
    protected void onCollisionBegin(Entity item, Entity player) {
        // 让道具消失
        item.removeFromWorld();
        // 拿到PlayerComponent
        final PlayerComponent playerComponent = player.getComponent(PlayerComponent.class);
        // 从entity获得道具类型
        final ItemType type = item.getObject(PropertyConst.ITEM_TYPE);
        switch (type) {
            // 吃到星星道具
            case STAR -> playerComponent.bulletLevelUp();
            // 吃到枪道具
            case GUN -> playerComponent.bulletLevel = GameConfig.BULLET_LEVEL_MAX;
            // 吃到爱心道具
            case HEART -> playerComponent.restore(GameConfig.ITEM_HEART);
            // 吃到船道具
            case SHIP -> playerComponent.giveArmedShip();
            // 吃到计时器道具
            case TIME -> {
                // 判断上次的定时器是否过期
                if (freezeEnemyTimerAction != null && !freezeEnemyTimerAction.isExpired()) {
                    // 如果没有过期则手动让他过期 会立即上个定时器中lambda里的代码
                    freezeEnemyTimerAction.expire();
                }
                // 获取场上所有敌人
                List<Entity> list = FXGL.getGameWorld().getGroup(GameEnum.ENEMY).getEntitiesCopy();
                // 让敌人停止update
                list.forEach(enemy -> enemy.setUpdateEnabled(false));
                // 若干秒之后让上次被暂停的敌人恢复行动
                freezeEnemyTimerAction = FXGL.runOnce(() -> list.forEach(enemy -> enemy.setUpdateEnabled(true)), GameConfig.FREEZE_ENEMY_TIME);
            }
            // 吃到头盔道具
            case HELMET -> playerComponent.giveInvincible();
            // 吃到坦克道具
            case TANK -> playerComponent.giveSpeeding();
            // 吃到炸弹道具
            case BOMB -> playerComponent.skillCount++;
            // 吃到铁铲道具
            case SPADE -> {
                MapFactory.clearHomeFence();
                spadeList = MapFactory.createHomeFence(SpawnsConst.STONE);
                FXGL.getGameTimer().runOnceAfter(() -> {
                    for (Entity entity : spadeList) {
                        if (entity.isActive()) {
                            entity.removeFromWorld();
                            FXGL.spawn(SpawnsConst.BRICK, entity.getPosition());
                        }
                    }
                }, GameConfig.HOME_FENCE_REINFORCE_TIME);
            }
        }

    }
}
